package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.AddMultiContentActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.jakewharton.rxbinding.view.RxView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class AddMultiContentActivity extends BaseActivity {
    private AddMultiContentActivityCustom dataBinding;
    private BaseQuickAdapter<String, BaseViewHolder> quickAdapter;
    private List<String> contents;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_multi_content);
        initTopTitleBar(View.VISIBLE, "标题");
        initTopRightText("完成", v -> {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < quickAdapter.getData().size(); i++) {
                stringBuilder.append(quickAdapter.getItem(i)+"-");
            }
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
            Intent intent = new Intent();
            intent.putExtra("content", stringBuilder.toString());
            setResult(Config.INTENT_CONTENT, intent);
            finish();
        });
    }

    @Override
    public void initData() {
    }

    @Override
    public void setView() {

        if (quickAdapter == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            quickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.list_item_14, new ArrayList<>()) {

                @Override
                protected void convert(BaseViewHolder helper, String item) {

                    helper.setText(R.id.tv_content, item);
                }
            };
            dataBinding.recyclerView.setAdapter(quickAdapter);
            if (contents != null) {
                ArrayList<String> temp = new ArrayList<>();
                temp.addAll(contents);
                quickAdapter.notifyDataChanged(temp, true);
                ItemClickSupport.addTo(dataBinding.recyclerView)
                        .setOnItemClickListener((rv, position, v) -> {

                        });
            }
        }
        RxView.clicks(dataBinding.tvAdd)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe((o) -> {
                    Bundle bundle = new Bundle();
                    bundle.putString("content", "");
                    bundle.putString("tip", "请填写内容");
                    bundle.putString("title", "添加内容");
                    startActivityForResult(EditTextActivity.class, bundle, Config.EDIT_CONTENT);
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.INTENT_CONTENT) {
            if (requestCode == Config.EDIT_CONTENT) {
                String content = data.getStringExtra("content");
                quickAdapter.addData(content);
            }
        }
    }
}
